package com.ykdl.member.kid.chat;

import java.util.ArrayList;

import oauth.signpost.http.HttpParameters;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.CommuBean;
import com.ykdl.member.kid.beans.CommuListBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.marketcard.CardScoreWapActivity;
import com.ykdl.member.kid.marketcard.IntegralDetailsActivity;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.views.IaskMenuBarView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class CommunicateAcivity extends BaseScreen{
	private  RefreshListView com_listview ;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 40;
	private IaskMenuBarView iask_menu_bar ;
	private HttpBaseRequest request ;
	private CommAdapter commAdapter;
	private boolean issetleftlist= false;
	private CommuListBean bean;
//	private ArrayList<SystemMessageBean> list = new ArrayList<SystemMessageBean>();
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("专属客服", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.activity_communicate);
		inint();
	}
	public void inint() {
		commAdapter=new CommAdapter(this);
		com_listview=(RefreshListView) findViewById(R.id.com_listview);
		
		com_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				CommuBean bean = (CommuBean)commAdapter.getItem(arg2);
				Intent intent = new Intent(mContext, ChatActivity.class);
				intent.putExtra(KidAction.COOPERATIVE_CORP_ID, bean.getPoint_type_id());
				intent.putExtra(KidAction.SEVRVER_NAME, bean.getName());
				CommunicateAcivity.this.startActivity(intent);
			}
		});
		com_listview.setMoreButtoIsGon(true);
		getSystemMessage();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		getSystemMessage();
		super.onResume();
	}
	
	public void getSystemMessage() {
		showProgress("正在加载中,请稍等...");
		String pageurl = KidConfig.COMMU_INDEX;
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, null);
		TaskManager.startHttpRequest(request, new getCommuDataTag(),  CommuListBean.class);
	}
	
	class getCommuDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if(t instanceof CommuListBean){
			    bean = (CommuListBean)t;
				if (null == bean.getError()) {
//					commAdapter.setlist(bean.getPoint_list());
					if(bean.getPoint_list().size()>0){
						com_listview.setAdapter(commAdapter);
						
					}else{
						com_listview.setVisibility(View.GONE);
//						showErrorTip("没有数据", "");
						findViewById(R.id.errtip_div).setVisibility(View.VISIBLE);
					}
					
				}else{
					Toast.makeText(mContext, "失败," + bean.getDesc(), 1).show();
				}
				
			}
			
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			return false;
		}

		@Override
		public void refreshCancel() {
			finishProgress();
			// TODO Auto-generated method stub
			
		}
		
	}
	class CommAdapter extends BaseAdapter{
		private BitmapUtils mBmpUtils;
		private Context mContext;
//		private ArrayList<CommuBean>  list;
		
		public CommAdapter(Context context){
			mContext=context;
			mBmpUtils = new BitmapUtils(context);
		}

//		public void setlist(ArrayList<CommuBean>  list) {
//			this.list = list ;
//		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return bean.getPoint_list().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return bean.getPoint_list().get(position-1);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder=null;
			if(convertView==null){
				convertView=RelativeLayout.inflate(mContext, R.layout.commadapter_item, null);
				holder=new ViewHolder();
				holder.pinpai_name=(TextView) convertView.findViewById(R.id.pinpai_name);
				holder.pinpai_icon=(ImageView) convertView.findViewById(R.id.pinpai_icon);
				holder.count=(ImageView) convertView.findViewById(R.id.count);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			holder.pinpai_name.setText(bean.getPoint_list().get(position).getBrand());
			String url="";
			if(bean.getPoint_list().get(position).getIcon()!=null){
				url=bean.getPoint_list().get(position).getIcon().getFile_url();
			}
			mBmpUtils.display(holder.pinpai_icon, url);
			if (bean.getPoint_list().get(position).getNew_message_count()==0) {
				holder.count.setVisibility(View.GONE);
			}else {
				holder.count.setVisibility(View.VISIBLE);
			}
			return convertView;
		}
		
		class ViewHolder{
			ImageView pinpai_icon,count;
			TextView pinpai_name;
		}
		
	}
	

}
